﻿using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace OneDark
{
    internal class TablesRdr
    {
        private readonly string filepath;
        private readonly List<TblCfg> tblCfgs = new List<TblCfg>();

        public TablesRdr(string file)
        {
            filepath = file;
        }

        public List<TblCfg> GetTbls()
        {
            FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);

            NPOI.XSSF.UserModel.XSSFWorkbook book = new NPOI.XSSF.UserModel.XSSFWorkbook(fs);

            //NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(fs);
            int sheetCount = book.NumberOfSheets;
            for (int sheetIndex = 0; sheetIndex < sheetCount; sheetIndex++)
            {
                NPOI.SS.UserModel.ISheet sheet = book.GetSheetAt(sheetIndex);
                if (sheet == null) continue;

                NPOI.SS.UserModel.IRow row = sheet.GetRow(0);
                if (row == null) continue;

                TblCfg tblCfg = new TblCfg
                {
                    TableName = row.GetCell(1).StringCellValue,  // 表名
                    ClassName = sheet.GetRow(1).GetCell(1).StringCellValue,  // 类名
                    FileName = sheet.GetRow(2).GetCell(1).StringCellValue,   // 文件名
                    NameSpace = sheet.GetRow(3).GetCell(1).StringCellValue  // 命名空间
                };

                for (int i = 5; i <= sheet.LastRowNum; i++)
                {
                    TblCol tblCol = new TblCol
                    {
                        ColIndex = (int)sheet.GetRow(i).GetCell(0).NumericCellValue,
                        ColDesc = sheet.GetRow(i).GetCell(1).StringCellValue,
                        ColName = sheet.GetRow(i).GetCell(2).StringCellValue
                    };

                    //数据类型处理
                    string dataType = sheet.GetRow(i).GetCell(3).StringCellValue;
                    if (dataType.Contains(' ')) //如果有空格，只取空格前面的
                    {
                        dataType = dataType.Substring(0, dataType.IndexOf(' '));
                    }

                    if (dataType.Contains("("))
                    {
                        tblCol.DataType = TblCol.GetSqlDbType(dataType.Substring(0, dataType.IndexOf('(')));
                        int perc = -1;
                        int leggth;
                        if (!dataType.Contains(",")) //只有长度
                        {
                            int.TryParse(dataType.Substring(dataType.IndexOf('(') + 1, dataType.LastIndexOf(')') - dataType.IndexOf('(') - 1).Trim(), out leggth);
                        }
                        else //既有长度又有精度
                        {
                            int.TryParse(dataType.Substring(dataType.IndexOf('(') + 1, dataType.LastIndexOf(',') - dataType.IndexOf('(') - 1).Trim(), out leggth);
                            int.TryParse(dataType.Substring(dataType.IndexOf(',') + 1, dataType.LastIndexOf(')') - dataType.IndexOf(',') - 1).Trim(), out perc);
                        }
                        tblCol.Length = leggth;
                        tblCol.Precision = perc;
                    }
                    else //只有类型
                    {
                        tblCol.DataType = TblCol.GetSqlDbType(dataType);
                    }

                    tblCol.Primary = sheet.GetRow(i).GetCell(4).StringCellValue.Contains("是");
                    tblCol.Nullable = sheet.GetRow(i).GetCell(5).StringCellValue.Contains("是");
                    tblCol.EnumType = sheet.GetRow(i).GetCell(6).StringCellValue;
                    tblCfg.TblCols.Add(tblCol);
                }

                tblCfgs.Add(tblCfg);
            }
            return tblCfgs;
        }
    }
}